﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            ConsoleAsync1();
            stopwatch.Stop();
            Console.WriteLine("同步方法用时：" + stopwatch.ElapsedMilliseconds);
            //stopwatch.Reset();
            //stopwatch.Start();
            //ConsoleAsync();
            //stopwatch.Stop();
            //Console.WriteLine("异步方法用时：" + stopwatch.ElapsedMilliseconds);
            Console.Read();
        }
        private static async void ConsoleAsync()
        {
            Console.WriteLine("异步方法开始");
            Console.WriteLine("Result:" + await SumAsync(10));
            Console.WriteLine("异步方法结束");
        }
        private static async Task<int> SumAsync(int part)
        {
            if ((part += 10) >= 100)
            {
                return 100;
            }
            HttpClient client = new HttpClient();
            Task<string> getStringTask = client.GetStringAsync("http://msdn.microsoft.com");
            Console.WriteLine(DateTime.Now.Millisecond + " 异步 " + (await getStringTask).Length);
            return await SumAsync(part);
        }

        private static void ConsoleAsync1()
        {
            Console.WriteLine("同步方法开始");
            Console.WriteLine("Result:" + SumAsync1(10));
            Console.WriteLine("同步方法结束");
        }

        private static int SumAsync1(int part)
        {
            if ((part += 10) >= 100)
            {
                return 100;
            }
            HttpClient client = new HttpClient();
            Task<string> getStringTask = client.GetStringAsync("http://msdn.microsoft.com");
          
            Console.WriteLine(DateTime.Now.Millisecond + " 同步 " + getStringTask.Result);
            return SumAsync1(part);
        }
    }
}
